import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import {getarchivesList} from '@/services/index'
export interface IndexModelState {
  list:Array<any>[]
}

export interface IndexModelType {
  namespace: 'archives';
  state: IndexModelState;
  effects: {
    getarchivesList: Effect;
  };
  reducers: {
    save: Reducer<IndexModelState>;
    // 启用 immer 之后
    // save: ImmerReducer<IndexModelState>;
  };
  subscriptions: { setup: Subscription };
}

const IndexModel: IndexModelType = {
  namespace: 'archives',

  state: {
    list:[]
  },

  effects: {
    *getarchivesList({ payload }, { call, put }) {
      let res = yield call(getarchivesList)
      yield put({
        type:'save',
        payload:{
          list:res.data
        }
      })
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/') {
          dispatch({
            type: 'query',
          });
        }
      });
    },
  },
};

export default IndexModel;